/*
 * @Author: 李环玉 834654733@qq.com
 * @Date: 2023-12-23 17:10:39
 * @LastEditors: 李环玉 834654733@qq.com
 * @LastEditTime: 2023-12-24 23:13:00
 * @FilePath: \koa-server\src\service\systemMeun.service.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const systemMenu = require('../model/systemMeun.model')
const { Op } = require("sequelize")
class systemMenuService {
  async getSystemMenu(params){
    const json = await systemMenu.findAll(
      { where:{ pId:null },
      include : {
        model: systemMenu,
        as:'children', 
        required : false,
        include : {
          model: systemMenu,
          all : true,
          nested : true,
          include : {
            all : true,
            nested : true,
            include : {
              all : true,
              nested : true,
              include : {
                all : true,
                nested : true,
                include : {
                  all : true,
                  nested : true,
                  include : {
                    all : true,
                    nested : true,
                    include : {
                      all : true,
                      nested : true,
                    }
                  }
                }
              }
            }
          }
        }
      },
      order:[['id']] 
    })
    return json
  }
  //分页查询菜单
  async getCMenu(params){
    let obj = Object.assign({},params)
    delete obj.pageNo
    delete obj.pageSize
    const { count, rows } = await systemMenu.findAndCountAll(
      {
        limit:params.pageSize,
        offset:(params.pageNo -1) * params.pageSize,
        where:{...obj} 
      },
    )
    return {
      rows,
      count
    }
  }
  //新增菜单
  async insert(params){
    const res = await systemMenu.create(params)
    return res
  }
  //修改菜单
  async updated(params) {
    const res = await systemMenu.update(
      {...params},
      { where:{id:params.id} }
    )
    return res
  }
  //删除菜单
  async deleteOne(params){
    const res = await systemMenu.destroy({ where:{ id:params.id } })
    return res
  }
  //查询一条信息
  async getOne(params){
    const res = await systemMenu.findOne({
      where:{ id:params.id }
    })
    return res
  }
}

module.exports = new systemMenuService()